import 'package:chewie/chewie.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:video_player/video_player.dart';
import 'index.dart';

class VideoPlayerViewController extends SuperController {
  VideoPlayerViewController();

  final VideoPlayerState state = VideoPlayerState();

  /// 在 widget 内存中分配后立即调用。
  @override
  void onInit() async {
    super.onInit();
    state.playerCtrl = VideoPlayerController.network(state.url);
  }

  /// 在 onInit() 之后调用 1 帧。这是进入的理想场所
  @override
  void onReady() {
    super.onReady();
  }

  Future<void> started() async {
    state.playerCtrl = VideoPlayerController.network(state.url);

    await state.playerCtrl.initialize();
    state.chewieCtrl = ChewieController(
      optionsTranslation: OptionsTranslation(cancelButtonText: '取消',playbackSpeedButtonText: '倍速'),
      videoPlayerController: state.playerCtrl,
      aspectRatio: state.playerCtrl.value.aspectRatio,
      autoPlay: true,
      looping: true,
      materialProgressColors: ChewieProgressColors(
        playedColor: Colors.white,
        bufferedColor: Colors.white60,
        handleColor: Colors.white,
      ),
      cupertinoProgressColors: ChewieProgressColors(
        playedColor: Colors.white,
        bufferedColor: Colors.white60,
        handleColor: Colors.white,
      ),
      // useRootNavigator: false,
      allowedScreenSleep: false,
    );
    update();
  }

  /// 在 [onDelete] 方法之前调用。
  @override
  void onClose() {
    // state.player.release();
    state.playerCtrl.dispose();
    state.chewieCtrl?.dispose();
    super.onClose();
  }

  /// dispose 释放内存
  @override
  void dispose() {
    super.dispose();
  }

  @override
  void onDetached() {
    print('-------onDetached');
  }

  @override
  void onInactive() {
    print('-------onInactive');
  }

  @override
  void onPaused() {
    print('-------onPaused');
    state.playerCtrl.pause();
  }

  @override
  void onResumed() {
    print('-------onResumed');
    state.playerCtrl.play();
    // TODO: implement onResumed
  }
}
